package com.school.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.admin.common.imit.InitDict;
import com.school.admin.entity.MallMenuRole;
import com.school.admin.entity.MallSysPurviewRole;
import com.school.admin.entity.vo.MallSysRoleVo;
import com.school.admin.mapper.MallMenuRoleMapper;
import com.school.admin.service.MallMenuRoleService;
import com.school.admin.service.MallSysPurviewRoleService;
import com.school.admin.service.MallSysPurviewService;
import com.school.admin.service.MallSysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单角色中间表 服务实现类
 * </p>
 *
 * @author sunpx
 * @since 2022-03-24
 */
@Service
public class MallMenuRoleServiceImpl extends ServiceImpl<MallMenuRoleMapper, MallMenuRole> implements MallMenuRoleService {

    @Autowired
    MallSysRoleService mallSysRoleService;


    @Autowired
    MallSysPurviewService mallSysPurviewService;

    @Autowired
    MallSysPurviewRoleService mallSysPurviewRoleService;

    @Autowired
    InitDict initDict;

    @Override
    public List<String> getFinallyModule(String id) {
        return baseMapper.getFinallyModule(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveMenuRoleService(MallSysRoleVo commonSave) {

        mallSysRoleService.saveOrUpdate(commonSave);

        QueryWrapper<MallMenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", commonSave.getId());
        boolean remove = remove(queryWrapper);


        QueryWrapper<MallSysPurviewRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", commonSave.getId());
        mallSysPurviewRoleService.remove(wrapper);
        mallSysPurviewRoleService.saveBatch(saveAPIList(commonSave));


        initDict.initPermission();
        return saveBatch(saveRoleList(commonSave));


    }


    /**
     * 封装角色与菜单绑定信息
     *
     * @param commonSave
     * @return java.util.List<com.school.admin.entity.MallMenuRole>
     * @author : sunpx
     * @date : 2022-03-26 19:20:53
     */
    public List<MallMenuRole> saveRoleList(MallSysRoleVo commonSave) {
        List<String> list = commonSave.getList();
        Long roleId = commonSave.getId();
        List<MallMenuRole> reList = new ArrayList<>();
        for (String s : list) {
            MallMenuRole menuRole = new MallMenuRole();
            menuRole.setMenuCode(s);
            menuRole.setRoleId(roleId);
            reList.add(menuRole);
        }
        return reList;
    }


    public List<MallSysPurviewRole> saveAPIList(MallSysRoleVo commonSave) {
        List<String> list = commonSave.getApiList();
        Long roleId = commonSave.getId();
        List<MallSysPurviewRole> reList = new ArrayList<>();
        for (String s : list) {
            MallSysPurviewRole menuRole = new MallSysPurviewRole();
            menuRole.setPurviewId(Long.valueOf(s));
            menuRole.setRoleId(roleId);
            menuRole.setCreateTime(new Date());
            reList.add(menuRole);
        }
        return reList;
    }
}
